#!/usr/bin/env node
'use strict';
var inquirer = require('inquirer');
require('shelljs/global');
require('locus');
var fuzzy = require('fuzzy');
var _ = require('lodash');
var fs = require('fs');

inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));


const files = fs.readdirSync('.').filter(v=>v.endsWith('.nomad')).map(v=>v.slice(0,-6))

inquirer.prompt([
  {
    type: 'autocomplete',
    name: 'file',
    message: 'choose job?',
    default: "",
    source: (answer, input)=>{
      input = input || ''
      const r = fuzzy.filter(input, files).map(v=>v.string)
      return Promise.resolve(r)
    }
  }
]).then(function (ans) {
  exec('nomad plan ' + ans.file + '.nomad')
  inquirer.prompt([
    {
      type: 'expand',
      message: 'run it? ',
      name: 'choose',
      choices: [
        {
          key: 'y',
          name: 'run',
          value: 'run'
        },
        {
          key: 'n',
          name: 'none',
          value: 'none'
        }
      ]
    }
  ]).then(function (answers) {
    if(answers.choose == 'run'){
      exec('nomad run ' + ans.file + '.nomad')
    }
  });

});

// eval(locus)
